package com.tz.online.entity;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name = "BS_ORDER")
public class Order {
	private Long id; // 订单ID标识
	private String orderNo; // 订单标号
	private Date createDate; // 下单日期
	private OrderStatus orderStatus; // 订单状态
	private double total; // 订单 总费用
	// 发货地址 [采用冗余的方式添加的属性]
	private String addr; // 省、市、区三级，使用,号分隔的字符串
	private String addrDetail; // 详细地址，除省、市、区三级外的详细地址
	// 订单收货人
	private String receiver;
	// 订单收货人电话
	private String phone;

	private Set<OrderItem> items; // 订单中的商品明细
	
	private User user; // 订单所属用户

	@OneToMany(mappedBy = "order", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	public Set<OrderItem> getItems() {
		return items;
	}

	public void setItems(Set<OrderItem> items) {
		this.items = items;
	}

	//内存中注册关联关系
	public void addItem(OrderItem item) {
		if(items == null) {
			items = new HashSet<>();
		}
		//注册
		this.items.add(item);
		//反向注册
		item.setOrder(this);
	}
	
	@Id
	@GeneratedValue(generator = "ord_id", strategy = GenerationType.SEQUENCE)
	@SequenceGenerator(name = "ord_id", sequenceName = "bs_ord_id")
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@Column
	public String getReceiver() {
		return receiver;
	}

	public void setReceiver(String receiver) {
		this.receiver = receiver;
	}

	@Column
	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	@Column(unique = true, updatable = false,nullable=false)
	public String getOrderNo() {
		return orderNo;
	}

	public void setOrderNo(String orderNo) {
		this.orderNo = orderNo;
	}

	@Column
	@Temporal(TemporalType.TIME)
	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	@Column
	@Enumerated(EnumType.ORDINAL)
	public OrderStatus getOrderStatus() {
		return orderStatus;
	}

	public void setOrderStatus(OrderStatus orderStatus) {
		this.orderStatus = orderStatus;
	}

	@Column
	public String getAddr() {
		return addr;
	}

	public void setAddr(String addr) {
		this.addr = addr;
	}

	@Column
	public String getAddrDetail() {
		return this.addrDetail;
	}

	public void setAddrDetail(String addrDetail) {
		this.addrDetail = addrDetail;
	}

	@Column
	public double getTotal() {
		return total;
	}

	public void setTotal(double total) {
		this.total = total;
	}

	@ManyToOne
	@JoinColumn(name = "USER_ID")
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("Order [id=").append(id).append(", orderNo=").append(orderNo).append(", createDate=")
				.append(createDate).append(", orderStatus=").append(orderStatus).append(", total=").append(total)
				.append(", addr=").append(addr).append(", addrDetail=").append(addrDetail).append(", receiver=")
				.append(receiver).append(", phone=").append(phone).append("]");
		return builder.toString();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((orderNo == null) ? 0 : orderNo.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Order other = (Order) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (orderNo == null) {
			if (other.orderNo != null)
				return false;
		} else if (!orderNo.equals(other.orderNo))
			return false;
		return true;
	}

	public Order() {
		super();
	}

}
